package org.keyroy.map.core.overlays;

import java.util.Properties;

import org.keyroy.map.core.Map;
import org.keyroy.map.core.MapOverlay;
import org.keyroy.map.core.sprite.Sprite;
import org.keyroy.mapEditor.ui.beans.MapUtil;
import org.keyroy.util.tag.Tag;

public class TilesOverlay extends MapOverlay {
	public String name;
	public Sprite[][] tiles;
	public Properties properties;

	public TilesOverlay() {
	}

	public TilesOverlay(Map map) {
		super(map);
	}

	@Override
	protected void init(Map map) {
		tiles = new Sprite[map.getColumns()][map.getRows()];
	}

	public Sprite get(int x, int y) {
		return tiles[x][y];
	}

	public boolean isDirty() {
		if (tiles != null) {
			for (int i = 0; i < tiles.length; i++) {
				for (int j = 0; j < tiles[i].length; j++) {
					if (tiles[i][j] != null && tiles[i][j].ID != Sprite.SPRITE_DELETE_ID) {
						return true;
					}
				}
			}
		}

		return false;
	}

	@Override
	public void initTag(Tag tag) {
		tag.addParameter("name", name, name != null);

		String dataString = MapUtil.tiles2String(tiles);
		tag.addParameter("data", dataString, dataString != null);
	}

	@Override
	public void initObj(Tag tag) {
		name = tag.getParameterValue("name");

		String dataString = tag.getParameterValue("data");
		if (dataString != null && tiles != null) {
			MapUtil.string2Tiles(tiles, dataString);
		}
	}

}
